import React, { Component } from 'react'

import { connect } from 'react-redux'
import { putDataAction } from './store/actionCreator'

const mapDispatchToProps = (dispatch) => {
  return {
    putData(task) {
      dispatch(putDataAction(task))
    }
  }
}

class Form extends Component {
  state = {
    task: ''
  }

  handleChange = (e) => {
    this.setState({
      task: e.target.value
    })
  }

  addItem = (e) => {
    if (e.keyCode === 13) {
      this.props.putData(this.state.task)
      this.setState({
        task: ''
      })
    }
  }
  render() {
    return (
      <div>
        <input type="text" value={this.state.task} onChange={this.handleChange} onKeyUp={this.addItem} />
      </div>
    )
  }
}

export default connect(null, mapDispatchToProps)(Form)
